package com.touchfired.common.utils;

import com.touchfired.common.constants.ClientInfo;
import com.touchfired.common.constants.RoomInfo;
import com.touchfired.common.context.ServerContext;
import com.touchfired.common.params.RoomRespInfo;
import com.touchfired.common.spring.BeanLocator;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 房间工具类
 *
 * @author 幸佳俊
 */
public class RoomUtil {

    /**
     * 房间信息转房间客户端信息实体
     * @param roomInfo 房间信息
     * @return 房间响应实体
     */
    public static RoomRespInfo converter(RoomInfo roomInfo) {
        RoomRespInfo roomRespInfo = null;
        if (roomInfo != null) {
            ServerContext serverContext = BeanLocator.getBean(ServerContext.class);
            if (serverContext != null) {
                roomRespInfo = new RoomRespInfo();
                BeanUtils.copyProperties(roomInfo, roomRespInfo);
                setRoomClientInfo(roomRespInfo);
            }
        }
        return roomRespInfo;
    }

    /**
     * 房间信息列表转房间客户端信息实体列表
     * @param roomInfos 房间信息列表
     * @return 房间客户端信息实体列表
     */
    public static List<RoomRespInfo> converters(List<RoomInfo> roomInfos) {
        List<RoomRespInfo> result = null;
        if (CollectionUtils.isNotEmpty(roomInfos)) {
            result = roomInfos.stream().map(RoomUtil::converter).collect(Collectors.toList());
        }
        return result;
    }

    /**
     * 设置房间内客户端信息
     * @param roomRespInfo 房间响应信息
     */
    public static void setRoomClientInfo(RoomRespInfo roomRespInfo) {
        if (roomRespInfo != null) {
            String createClientLongId = roomRespInfo.getCreateClientLongId();
            if (StringUtils.isNotBlank(createClientLongId)) {
                ServerContext serverContext = BeanLocator.getBean(ServerContext.class);
                if (serverContext != null) {
                    // 创建人
                    ClientInfo clientInfo = serverContext.getClientInfo(roomRespInfo.getCreateClientLongId());
                    roomRespInfo.setClientInfo(clientInfo);

                    // 房间内所有人
                    List<String> roomClientLongIds = roomRespInfo.getRoomClientLongIds();
                    List<ClientInfo> roomClients = getRoomClients(roomClientLongIds);
                    roomRespInfo.setRoomClients(roomClients);
                }
            }
        }
    }

    /**
     * 获取房间客户端信息
     * @param roomClientLongIds 房间客户端ids
     * @return 房间客户端信息
     */
    public static List<ClientInfo> getRoomClients(List<String> roomClientLongIds) {
        List<ClientInfo> clientInfoList = new LinkedList<>();
        if (CollectionUtils.isNotEmpty(roomClientLongIds)) {
            ServerContext serverContext = BeanLocator.getBean(ServerContext.class);
            if (serverContext != null) {
                for (String roomClientLongId : roomClientLongIds) {
                    ClientInfo roomClientInfo = serverContext.getClientInfo(roomClientLongId);
                    clientInfoList.add(roomClientInfo);
                }
            }
        }
        return clientInfoList;
    }

}
